package cn.com.thinvent.dispatch.web.controller;

import cn.com.thinvent.dispatch.entity.ModelResult;
import cn.com.thinvent.dispatch.entity.ModelTask;
import cn.com.thinvent.dispatch.entity.request.TaskDTO;
import cn.com.thinvent.dispatch.entity.response.*;
import cn.com.thinvent.dispatch.service.biz.ModelTaskService;
import cn.com.thinvent.dispatch.web.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Task的controller
 */
@Controller
@RequestMapping("/task")
public class ModelTaskController {

    @Autowired
    private ModelTaskService modelTaskService;

    /**
     * 分页查询任务列表
     * @param taskName
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public ReturnModel list(@RequestParam(value = "taskName", required = false) String taskName,
                            @RequestParam("pageNum") Integer pageNum,
                            @RequestParam("pageSize") Integer pageSize) {
        PagedGridResult<ModelTaskResponse> list = modelTaskService.list(UserContext.get(), taskName, pageNum, pageSize);
        return new ReturnModel(200, list, "");
    }

    /**
     * 新增或修改
     * @param taskDTO
     * @return
     */
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public ReturnModel saveOrUpdate(@RequestBody TaskDTO taskDTO) {
        System.out.println(taskDTO);
        taskDTO.setUserId(UserContext.get());
        ModelTask result = modelTaskService.saveOrUpdate(taskDTO);
        return new ReturnModel(200, result, "");
    }

    /**
     * 立即执行
     * @param taskId
     * @return
     */
    @GetMapping("/actionNow")
    @ResponseBody
    public ReturnModel actionNow(@RequestParam("taskId") Integer taskId) {
        modelTaskService.actionNow(taskId, UserContext.get());
        return new ReturnModel(200, "", "");
    }

    /**
     * 删除
     * @param taskId
     * @return
     */
    @GetMapping("/delete")
    @ResponseBody
    public ReturnModel delete(@RequestParam("taskId") Integer taskId) {
        Integer delete = modelTaskService.delete(taskId);
        return new ReturnModel(200, delete, "");
    }

    /**
     * 查看执行结果
     * @param taskId
     * @return
     */
    @GetMapping("/actionResult")
    @ResponseBody
    public ReturnModel actionResult(@RequestParam("taskId") Integer taskId) {
        ActionResultResponse actionResultResponse = modelTaskService.actionResult(taskId);
        return new ReturnModel(200, actionResultResponse, "");
    }

    /**
     * 执行结果详情
     * @param taskId
     * @return
     */
    @GetMapping("/resultDetail")
    @ResponseBody
    public ReturnModel resultDetail(@RequestParam("taskId") Integer taskId,
                                    @RequestParam("pageNum") Integer pageNum,
                                    @RequestParam("pageSize") Integer pageSize) {
        ResultDetailResponse response = modelTaskService.resultDetail(taskId, pageNum, pageSize);
        return new ReturnModel(200, response, "");
    }

    /**
     * 重新执行
     * @param taskId
     * @return
     */
    @GetMapping("/resActionNow")
    @ResponseBody
    public ReturnModel resActionNow(@RequestParam("taskId") Integer taskId) {
        modelTaskService.resActionNow(taskId, UserContext.get());
        return new ReturnModel(200, "", "");
    }

    /**
     * 继续执行
     * @param taskId
     * @return
     */
    @GetMapping("/continueActionNow")
    @ResponseBody
    public ReturnModel continueActionNow(@RequestParam("taskId") Integer taskId) {
        modelTaskService.continueActionNow(taskId, UserContext.get());
        return new ReturnModel(200, "", "");
    }

    /**
     * 执行结果列表
     * @param taskId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/resultPageList")
    @ResponseBody
    public ReturnModel resultPageList(@RequestParam("taskId") Integer taskId,
                                      @RequestParam("pageNum") Integer pageNum,
                                      @RequestParam("pageSize") Integer pageSize) {
        PagedGridResult<ModelResult> pagedGridResult = modelTaskService.resultPageList(taskId, pageNum, pageSize);
        return new ReturnModel(200, pagedGridResult, "");
    }

    /**
     * 结果详情页面
     * @param taskId
     * @param resultId
     * @return
     */
    @GetMapping("/resultPageDetail")
    @ResponseBody
    public ReturnModel resultPageDetail(@RequestParam("taskId") Integer taskId,
                                        @RequestParam("resultId") Integer resultId) {
        ResultPageDetailResponse response = modelTaskService.resultPageDetail(taskId, resultId);
        return new ReturnModel(200, response, "");
    }

    /**
     * 修改结果
     * @return
     */
    @PostMapping("/updateResult")
    @ResponseBody
    public ReturnModel updateResult(@RequestBody ModelResult modelResult) {
        modelTaskService.updateResult(modelResult.getId(), modelResult.getResult());
        return new ReturnModel(200, "", "");
    }
}
